<template>
	<view>
		<!-- #ifdef MP-WEIXIN -->
		<cu-custom bgColor="bg-red" :isBack="true">		<block slot="content">搜索</block>
		</cu-custom>
		<!-- #endif -->
		<view class="cu-bar search  appbg bg-white">
			<!-- #ifdef H5 -->
			<view class="action" @tap="BackPage">
				<text class="cuIcon-back"></text>
				<slot name="backText"></slot>
			</view>
			<!-- #endif -->
			<view class="content">渠道进货</view>
		</view>
		<view class="VerticalBox">
			<scroll-view class="VerticalNav nav bg-white" scroll-y scroll-with-animation :scroll-top="verticalNavTop">
				<view class="cu-item leftitem" :class="index==tabCur?'text-black text-bold text-lg cur':''" v-for="(item,index) in list"
				 :key="index" @tap="TabSelect" :data-id="index">
					<view class="after" :class="'bg-'+theme"></view>
					{{item.title}}
				</view>
			</scroll-view>
			<scroll-view class="VerticalMain" scroll-y scroll-with-animation>
				<block v-for="(item,index) in list" :key="index">
					<view class="padding-top-sm padding-left-sm " :id="'main-'+index" v-if="tabCur==index">
						<view class="cu-list grid col-3 padding no-border ">
							<view class="cu-item" v-for="(cate,i) in item['child']" @tap="goCategoryList(cate)" :key="i">
								<image class="cu-avatar xl margin-center bg-white" mode="aspectFit" :src="cate.cover"></image>
								<view class="content padding-top-xs">
									{{cate.title}}
								</view>
							</view>
						</view>
					</view>
				</block>
			</scroll-view>
		</view>

		<!-- <Navbar PageCur="channel_cate"></Navbar> -->
	</view>
</template>

<script>
	import Navbar from '@/components/default/Navbar.vue'
	export default {
		components: {
			Navbar
		},
		data() {
			return {
				type: 2,
				list: [],
				tabCur: 0,
				mainCur: 0,
				verticalNavTop: 0,
				load: true,
				theme: this.$config.THEME(),
			};
		},
		onLoad() {
			let list = this.$common.getCache('category_' + this.type)
			if (!list) {
				this.$nextTick((r) => {
					uni.startPullDownRefresh({})
				})
			} else {
				this.list = list
			}
		},
		onShow() {
			this.theme = this.$config.THEME()
		},
		onReady() {
			uni.hideLoading()
		},
		methods: {
			TabSelect(e) {
				this.tabCur = e.currentTarget.dataset.id;
				this.mainCur = e.currentTarget.dataset.id;
				this.verticalNavTop = (e.currentTarget.dataset.id - 1) * 50
			},
			getCategory() {
				this.$http
					.get('product_cate', {
						type: this.type
					})
					.then((response) => {
						console.log(response)
						this.list = response.data.data
						this.$common.setCache('category_' + this.type, this.list, 2 * 60)
						// this.initSelect()
						uni.hideLoading()
						uni.stopPullDownRefresh()
					})
					.catch((response) => {
						console.log(response)
					})
			},
			VerticalMain(e) {
				// #ifdef MP-ALIPAY
				return false //支付宝小程序暂时不支持双向联动 
				// #endif
				// let that = this;
				// let tabHeight = 0;
				// if (this.load) {
				// 	for (let i = 0; i < this.list.length; i++) {
				// 		let view = uni.createSelectorQuery().select("#main-" + this.list[i].id);
				// 		view.fields({
				// 			size: true
				// 		}, data => {
				// 			this.list[i].top = tabHeight;
				// 			tabHeight = tabHeight + data.height;
				// 			this.list[i].bottom = tabHeight;
				// 		}).exec();
				// 	}
				// 	this.load = false
				// }
				// let scrollTop = e.detail.scrollTop + 10;
				// for (let i = 0; i < this.list.length; i++) {
				// 	if (scrollTop > this.list[i].top && scrollTop < this.list[i].bottom) {
				// 		this.verticalNavTop = (this.list[i].id - 1) * 50
				// 		this.tabCur = this.list[i].id
				// 		console.log(scrollTop)
				// 		return false
				// 	}
				// }
			},
			goCategoryList(item) {
				uni.navigateTo({
					url: "/pages/shop/list?cate_id=" + item.id + "&type=" + this.type
				})
			},
			BackPage() {
				uni.navigateBack({
					delta: 1
				});
			}
		},
		onPullDownRefresh() {
			this.list = []
			this.getCategory()
		},
	}
</script>

<style>
	.fixed {
		position: fixed;
		z-index: 99;
	}

	.VerticalNav.nav {
		width: 160upx;
		white-space: initial;
	}

	.VerticalNav.nav .cu-item {
		width: 100%;
		text-align: center;
		background-color: #fff;
		margin: 0;
		border: none;
		height: 50px;
		position: relative;
	}

	.VerticalNav.nav .cu-item.cur {}

	.VerticalNav.nav .cu-item.cur .after {
		content: "";
		width: 8upx;
		height: 30upx;
		border-radius: 0 10upx 10upx 0;
		position: absolute;
		/* background-color: #3D7EFF; */
		top: 0;
		left: 0upx;
		bottom: 0;
		margin: auto;
	}

	.nav .cu-item.cur::after {
		display: none;
	}

	.VerticalBox {
		height: 92vh;
		display: flex;
	}

	.VerticalMain {
		background-color: #f1f1f1;
		flex: 1;
	}

	.leftitem {
		height: auto !important;
		line-height: 40rpx;
		padding: 30rpx 15rpx;
	}

	.main-list {
		min-height: 100vh;
	}
</style>
